A1: tips for matploblib

matplotlib
Author

최규빈

Published

September 1, 2022

matplotlib 미세먼지팁

그림만 보고 싶을때

plt.plot([1,2,3,4],[2,3,4,5]);

marker size, line width

plt.plot([1,2,3,4],[2,3,4,2],'o',ms=10)

plt.plot([1,2,3,4],[2,3,4,5],'--',lw=10)

label + legend

plt.plot([1,2,3,4],[1,2,3,2],'--o',label='A')
plt.plot([1,2,3,4],[3,2.1,1,3],'--o',label='B')
plt.legend()
<matplotlib.legend.Legend at 0x7f5889ead210>

색깔조정 (C0,C1,…)

plt.plot([1,2,3,4],[1,2,3,2],'--o',label='A',color='C1')
plt.plot([1,2,3,4],[3,2.1,1,3],'--o',label='B',color='C0')
plt.legend()
<matplotlib.legend.Legend at 0x7f588a29ef10>

title 설정

- (방법1)

plt.plot([1,2,3,4],[1,2,3,2],'--o',label='A',color='C1')
plt.plot([1,2,3,4],[3,2.1,1,3],'--o',label='B',color='C0')
plt.legend()
plt.title('title')
Text(0.5, 1.0, 'title')

- (방법2)

fig, ax = plt.subplots()
ax.plot([1,2,3,4],[1,2,3,2],'--o',label='A',color='C1')
ax.plot([1,2,3,4],[3,2.1,1,3],'--o',label='B',color='C0')
ax.legend()
ax.set_title('title')
Text(0.5, 1.0, 'title')

suptitle 설정

fig, ax = plt.subplots(2,2)
ax[0,0].plot([1,2,3,2],'--o',label='A',color='C0')
ax[0,0].set_title('(a)')
ax[0,1].plot([3,2.1,1,3],'--o',label='B',color='C1')
ax[0,1].set_title('(b)')
ax[1,0].plot([-3,-2.1,-1,-3],'--o',label='B',color='C2')
ax[1,0].set_title('(c)')
ax[1,1].plot([3,-2.1,1,-3],'--o',label='B',color='C3')
ax[1,1].set_title('(d)')
#plt.suptitle('suptitle')
fig.suptitle('suptitle')
Text(0.5, 0.98, 'suptitle')

tight_layout()

fig

fig.tight_layout()
fig

fig, ax, plt 소속

- 일단 그림 하나 그리고 이야기좀 해보자.

fig, ax = plt.subplots()
ax.plot([1,2,3,1])

- fig에는 있고 ax에는 없는 것

add_axes, tight_layout, suptitle, …

- ax에는 있고 fig에는 없는 것

boxplot, hist, plot, set_title, …

- plt는 대부분 다 있음. (의미상 명확한건 대충 알아서 fig, ax에 접근해서 처리해준다) - plt.tight_layout, plt.suptitle, plt.boxplot, plt.hist, plot.plot - plt.set_title 은 없지만 plt.title 은 있음 - plt.add_axes 는 없음..

x축, y축 label 설정

ax.xaxis.set_label_text('xlabel',size=16,family='serif',weight=1000,style='italic')
#_fontsettings={'size':16,'family':'serif','weight'=1000,'style':'italic'}
#ax.xaxis.set_label_text('xlabel',_fontsettings)
fig

폰트ref - size: - fontweight: 0~1000 - family: ‘serif’, ‘sans-serif’, ‘monospace’ - style: ‘normal’, ‘italic’

ax.set_ylabel('ylabel',size=16)
fig

Latex

- 예시1

import numpy as np
import matplotlib.pyplot as plt
x1= np.linspace(-2,2,1000)
y1= (x1-1)**2 
fig, ax = plt.subplots()
ax.plot(x1,y1,'--')
ax.set_title('$y_1=(x_1-1)^2$')
Text(0.5, 1.0, '$y_1=(x_1-1)^2$')

- 예시2

x1 = np.linspace(-2,2,1000)
y1 = 0.5*(x1-1)**2 
fig, ax = plt.subplots()
ax.plot(x1,y1,'--')
ax.set_title(r'$y_1=\frac{1}{2}(x_1-1)^2$',size=20);

- 예시3

x1 = np.linspace(-2,2,1000)
y1 = 0.5*(x1-1)**2 
fig, ax = plt.subplots()
ax.plot(x1,y1,'--')
ax.set_title(r'$y_1=\frac{1}{2}(x_1-1)^2$',size=20)
ax.set_xlabel(r'$x_1$',size=15)
ax.set_ylabel(r'$y_1$',size=15);

- 예시4

x1 = np.linspace(-2,2,1000)
y1 = 0.5*(x1-1)**2 
y2 = 0.5*(x1+1)**2
fig, ax = plt.subplots()
ax.plot(x1,y1,'--',label=r'$\frac{1}{2}(x-1)^2$')
ax.plot(x1,y2,'--',label=r'$\frac{1}{2}(x+1)^2$')
ax.legend()
<matplotlib.legend.Legend at 0x7f0076915510>

fig.subplots()

fig,ax = plt.subplots(2,2)
ax[0,0].plot([1,2,4,3],'o',color='C0')
ax[0,1].plot([1,2,4,3],'o',color='C1')
ax[1,0].plot([1,2,4,3],'o',color='C2')
ax[1,1].plot([1,2,4,3],'o',color='C3')

fig = plt.figure()
ax = fig.subplots(2,2)
ax[0,0].plot([1,2,4,3],'o',color='C0')
ax[0,1].plot([1,2,4,3],'o',color='C1')
ax[1,0].plot([1,2,4,3],'o',color='C2')
ax[1,1].plot([1,2,4,3],'o',color='C3')

plt.subplot

  • 끝에 s가 없어요!!

- 기능1: 몰라도 됩니당.. (아마도)

- 기능2: 특이해요.. fig를 안받아도 무방함

ax1 = plt.subplot(221) 
ax2 = plt.subplot(222)
ax3 = plt.subplot(223)
ax4 = plt.subplot(224)

ax1.plot([1,2,4,3],'o',color='C0');ax1.set_title('221')
ax2.plot([1,2,4,3],'o',color='C1');ax2.set_title('222')
ax3.plot([1,2,4,3],'o',color='C2');ax3.set_title('223')
ax4.plot([1,2,4,3],'o',color='C3');ax4.set_title('224')

fig=plt.gcf()
fig.suptitle("plt.subplot(22x)",size=15)
fig.tight_layout()

위는 아래와 같은 코드임

fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)

ax1.plot([1,2,4,3],'o',color='C0');ax1.set_title('221')
ax2.plot([1,2,4,3],'o',color='C1');ax2.set_title('222')
ax3.plot([1,2,4,3],'o',color='C2');ax3.set_title('223')
ax4.plot([1,2,4,3],'o',color='C3');ax4.set_title('224')

fig.tight_layout()
fig.suptitle("fig.add_subplot(22x)",size=15)
fig.tight_layout()

- fig.add_subplot() vs fig.add_axes()

  • fig.add_subplot(): 입력으로 nrows, ncols, index 전달 (편하게 쓰기엔 좋아)
  • fig.add_axes(): 입력으로 left, bottom, width, height 전달 (이상한 그래프 만들기 좋아)

- plt.subplots() vs plt.subplot()

  • plt.subplots(): 넣을 수 있는 액시즈 종류가 한가지
  • plt.subplot(): 여러 (특이한) 액시즈를 넣을 수 있음

(기본액시즈)

plt.subplot(111,projection=None)
<AxesSubplot:>

(3d 액시즈)

ax=plt.subplot(111,projection='3d')
ax.plot([1,2,3,4],[1,2,-3,4],[1,2,-3,-4],'--o')
fig=plt.gcf()
fig.set_figheight(12)

(polar 액시즈)

ax=plt.subplot(111,projection='polar')
r = np.linspace(0,5,100)
theta = np.linspace(0,2*np.pi,100)
ax.plot(theta,r)